package org.jaaksi.view.filterbar;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Fcy on 2016/5/10. 这个只是筛选条部分，并不包含筛选panel
 *
 */
public class FilterBarController implements FilterBarUIHandler
{
	private Context mContext;
	private View filterBarView;
	private FilterBarHandler mFilterBarHandler;

	@Bind(R.id.tv_filter_1)
	TextView tv_filter_1;
	@Bind(R.id.tv_filter_2)
	TextView tv_filter_2;
	@Bind(R.id.tv_filter_3)
	TextView tv_filter_3;
	@Bind(R.id.tv_filter_4)
	TextView tv_filter_4;

	// @Bind(R.id.container_filter_panel)
	// private View container_filter_panel;
	// @Bind(R.id.ll_panel_area)
	// private View ll_panel_area;
	// @Bind(R.id.ll_panel_house_type)
	// private View ll_panel_house_type;

	private FilterBarController(Context context, View view, String[] filterArr,
	        @Nullable FilterBarHandler filterBarHandler)
	{
		mContext = context;
		bindView(view, filterArr, filterBarHandler);
	};

	/**
	 * @param filterBarView
	 *            筛选条
	 * @param filterArr
	 *            4各个筛选条默认文案
	 * @param filterBarHandler
	 * @return
	 */
	public static FilterBarController getInstance(Context context,
	        View filterBarView, String[] filterArr,
	        @NonNull FilterBarHandler filterBarHandler)
	{
		return new FilterBarController(context, filterBarView, filterArr,
		        filterBarHandler);
	}

	@OnClick({ R.id.ll_filter_1, R.id.ll_filter_2, R.id.ll_filter_3,
	        R.id.ll_filter_4 })
	protected void clickFilter(View view)
	{
		if (mFilterBarHandler != null)
		{
			switch (view.getId())
			{
				case R.id.ll_filter_1:
					mFilterBarHandler.filterAction(1);
					break;
			}
		}
	}

	private void bindView(View view, String[] filterArr,
	        @Nullable FilterBarHandler filterBarHandler)
	{
		mFilterBarHandler = filterBarHandler;
		filterBarView = view;
		ButterKnife.bind(this, view);
		array = filterArr;
	}

	// protected void showPanel(View view)
	// {
	// hideAllPanel();
	// view.setVisibility(View.GONE);
	// container_filter_panel.setVisibility(View.GONE);
	// }
	//
	// protected void hideAllPanel()
	// {
	// container_filter_panel.setVisibility(View.GONE);
	// ll_panel_area.setVisibility(View.GONE);
	// ll_panel_house_type.setVisibility(View.GONE);
	// }

	// // 含有 tag,当用户post事件时,只有指定了"tag"的事件才会触发该函数,执行在UI线程
	// @Subscriber(tag = ConstantUtils.IEventTag.TAG_FILTER_MORE)
	// public void changeMoreFilterIndex(
	// FilterIndexInfo.MoreIndexInfo moreIndexInfo)
	// {
	// }

	private String[] array;

	// private void initFilterTextArr(Context context, int arrId)
	// {
	// array = context.getResources().getStringArray(arrId);
	// }

	/**
	 * 重设筛选条文案UI,不要设置position=-1&&text!=null
	 * 
	 * @param position
	 *            -1表示重置所有
	 * @param text
	 *            null表示重置，否则表示设置
	 */
	private void resetUI(int position, String text)
	{
		switch (position)
		{
			case -1:
			case 1:
				if (text == null)
				{ // 默认值，默认颜色
					tv_filter_1.setText(array[0]);
					tv_filter_1.setTextColor(mContext.getResources()
					        .getColor(R.color.light_black));
					// TODO 设置图标为默认图片
				}
				else
				{
					tv_filter_1.setText(text);
					tv_filter_1.setTextColor(
					        mContext.getResources().getColor(R.color.bg_title));
					// TODO 设置图标为选中图片
				}

				if (position != -1)
				{
					break;
				}
			case 2:
				if (position != -1)
				{
					break;
				}
			case 3:
				if (position != -1)
				{
					break;
				}
			case 4:
				if (position != -1)
				{
					break;
				}
		}
	}

	@Override
	public void updateFilterUI(int position, String text)
	{
		resetUI(position, text);
	}
}
